﻿/*
 * Copyright 2020, Guan Xiaopeng
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
 
using System;
using System.Net;

namespace XPRPC
{
    /// <summary>
    /// 服务描述符，用于向 ServiceManager 声明一个服务
    /// </summary>
    [Serializable]
    public sealed class ServiceDescriptor
    {
        /// <summary>
        /// 服务名称，在 ServiceManager 中唯一标志一个服务
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 服务接口名称
        /// </summary>
        public string InterfaceName { get; set; }

        /// <summary>
        /// 服务描述文字
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 服务运行的主机名，客户端通过ServiceHost/ServicePort来连接服务
        /// </summary>
        public string ServiceHost {get; set;}
        /// <summary>
        /// 服务运行的端口，客户端通过ServiceHost/ServicePort来连接服务
        /// </summary>
        public int ServicePort { get; set; }

        /// <summary>
        /// 服务Tcp连接是否使用SSL加密连接
        /// </summary>
        public bool UseSSL {get; set;} = false;

        /// <summary>
        /// 安全认证令牌，由Service产生，在ServiceManager中登记。
        /// 客户端需要从 @IServiceManager.GetService 获取这个令牌来访问 Service。
        /// Service 只允许正确提供此令牌的客户端访问其他API。
        /// </summary>
        public string AccessToken { get; set; }

        public override string ToString()
        {
            return $"{Name}<{InterfaceName}>@{ServiceHost}:{ServicePort}";
        }

        public bool IsIdentityWith(ServiceDescriptor other)
        {
            return this.ToString() == other.ToString();
        }
    }
}

